package dao;

import entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.JdbcUtilsV1;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDao {
    public User login(String username, String password) throws SQLException {
        // 操作数据库
        Connection connection = JdbcUtilsV1.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from user where username=? and password=?";
        BeanHandler<User> userBeanHandler = new BeanHandler<>(User.class);
        return queryRunner.query(connection,sql,userBeanHandler,username,password);
    }

    public List<User> listUsers() {
        // 操作数据库
        try {
            Connection connection = JdbcUtilsV1.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            String sql = "select * from user";
            return queryRunner.query(connection,sql,new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
